<template>
	<view class="tab">
		<scroll-view class="tab_scroll" scroll-x>
			<view class="tab_scroll_box">
				<view v-for="(item,index) in list" class="tab_scroll_item" :class="{active:activeIndex===item.id}"
					@click="clickTab(item)">
					{{item.name}}
				</view>
			</view>
		</scroll-view>
		<view class="tab_icon">
			<uni-icons type="gear" size="26" color="#666"></uni-icons>
		</view>
	</view>
</template>

<script>
	export default {
		name: "tab",

		props: {
			list: {
				type: Array,
				default () {
					return []
				}
			},
			tabIndex: {
				type: Number,
				default: 0,
			}
		},
		//watch的方法是可以监听props，data里面值的变化的
		watch: {
			tabIndex(newval, oldval) {
				this.activeIndex=newval
			}
		},
		data() {
			return {
				activeIndex: 1,
			};
		},
		methods: {
			clickTab(item) {
				this.activeIndex = item.id
				//把数据传输到index界面
				//调用tab组件就会接受到这个事件
				this.$emit('tab', {
					data: item,
					index:item.id,
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../../common/css/icons.css';

	.tab {
		//让滚动的字和设置的图标在同一行
		display: flex;
		width: 100%;
		border-bottom: 1px solid #f5f5f5;
		background-color: #fff;
		box-sizing: border-box;

		.tab_scroll {
			flex: 1; //让tab撑满整个元素
			overflow: hidden;
			box-sizing: border-box;

			.tab_scroll_box {
				display: flex;
				height: 45rpx;
				align-items: center;
				//不会换行
				flex-wrap: nowrap;
				box-sizing: border-box;

				.tab_scroll_item {
					flex-shrink: 0;
					padding: 0 10px;
					color: #333;
					font-size: 14px;

					&.active {
						color: firebrick;
					}
				}
			}
		}

		.tab_icon {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 45px;

			&::after {
				content: "";
				position: absolute;
				top: 12px;
				bottom: 12px;
				left: 0;
				width: 1px;
			}
		}
	}
</style>